<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 20:28
 */
namespace App\Admin\Controllers;

use App\Admin\Traits\UserTrait;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Lib\Sdk\Manager;
use App\Lib\Sdk\Spider\PddSpider;
use App\Models\Group;
use App\Models\GroupConfig;
use App\Models\Robots;
use App\Models\System;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\String\u;

/**
 * 工具
 * Class ToolsController
 * @package App\Admin\Controllers
 */
class GroupsController extends Controller {

    use UserTrait;

    public function index(Content $content)
    {
        $uid = $this->getUserId();
        $model = new Group();
        return $content
            ->title("系统管理")
            ->body($this->grid($model));

    }

    protected function grid(Group $model)
    {
        $grid = new Grid($model);
        $grid->setTitle("微信群管理");
        $grid->model()->where("uid", $this->getUserId());
        Helper::gridLoadModel($grid, $model);
        $grid->actions(function(Grid\Displayers\Actions $actions) {
           $actions->disableEdit();
           $actions->disableView();
        });
        $grid->disableRowSelector();
        return $grid;
    }


    public function create(Content $content)
    {
        $model = new Group();
        $form = new Form($model);
        Helper::formLoadModel($form, $model);
        $form->disableViewCheck();
        return $content
            ->title("系统管理")
            ->body($form);
    }


    public function store(Request $request)
    {
        DB::beginTransaction();
        try {
            $data = $request->post();
            $count = Group::query()->where("uid", $this->getUserId())->count();
            $robots = Robots::query()->where("user_id", $this->getUserId())->first();
            if ($robots->group_max <= $count) {
                throw new \Exception(sprintf("最高可以设置%d个群，现有%d个" , $robots->group_max, $count));
            }
            $robot = Manager::getSdk();
            $wxid = User::getWxid();
            $groupId = $robot->findGroupByName($data['name'], $wxid);
            if ($groupId == false) {
                throw new \Exception("找不到该群聊");
            }
            $model = new Group();
            $model->setAttribute("name", $data['name']);
            $model->setAttribute("uid", $this->getUserId());
            $model->setAttribute("wxid", $groupId);
            $model->save();
            /*
            $robots = Robots::where("user_id", $this->getUserId())->first();
            $res = $robot->createGroup($data['name'], $robots);
            if ((bool)$res->Success){
                $model = new Group();
                $model->setAttribute("name", $data['name']);
                $model->setAttribute("uid", $this->getUserId());
                $model->save();
            //    $res2 = $robot->send($robots, "创建成功，你可以在这里发消息了", [$model->name]);*/
           /*
            } else {
                throw new \Exception($res->Message);
            }*/
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
        return redirect("/admin/groups/index");
    }



}